package com.sumer.psychology.friend.application.command;

import org.springframework.transaction.annotation.Transactional;

import com.sumer.psychology.common.Assert;
import com.sumer.psychology.friend.domain.model.Member;
import com.sumer.psychology.friend.domain.model.MemberId;
import com.sumer.psychology.friend.domain.model.MemberRepository;
import com.sumer.psychology.friend.domain.service.FriendshipService;

/**
 * 朋友关系应用服务
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/8/28
 * Time: 16:34
 */
@Transactional
public class FriendshipApplicationService {
    private FriendshipService friendshipService;
    private MemberRepository memberRepository;

    public FriendshipApplicationService(FriendshipService friendshipService, MemberRepository memberRepository) {
        this.friendshipService = friendshipService;
        this.memberRepository = memberRepository;
    }

    /**
     * 申请一个朋友
     *
     * @param requestFriendCommand command
     */
    public void requestFriend(RequestFriendCommand requestFriendCommand) {
        Member requester = aMember(requestFriendCommand.requestId());
        Member friend = aMember(requestFriendCommand.friendId());

        requester.requestFriend(
                friend,
                requestFriendCommand.words());
    }

    /*
     * 获得一个成员
     */
    private Member aMember(String aMemberId) {
        Member aMember = memberRepository().memberOfId(new MemberId(aMemberId));

        if (aMember == null) {
            aMember = new Member(
                    new MemberId(aMemberId));
            memberRepository().add(aMember);
        }
        return aMember;
    }

    /**
     * 拒绝一个朋友
     *
     * @param rejectFriendCommand command
     */
    public void rejectFriend(RejectFriendCommand rejectFriendCommand) {
        Member requester = memberRepository().memberOfId(
                new MemberId(rejectFriendCommand.requesterId()));
        Member acceptor = memberRepository().memberOfId(
                new MemberId(rejectFriendCommand.acceptorId()));

        Assert.notNull(requester, "申请者不存在");
        Assert.notNull(acceptor, "接受者不存在");

        acceptor.rejectFriend(
                requester,
                rejectFriendCommand.words(),
                rejectFriendCommand.messageId());
    }

    /**
     * 确认一个朋友关系
     *
     * @param confirmFriendshipCommand command
     */
    public void confirmFriendship(ConfirmFriendshipCommand confirmFriendshipCommand) {
        friendshipService().confirmFriendshipBetween(
                new MemberId(confirmFriendshipCommand.requesterId()),
                new MemberId(confirmFriendshipCommand.acceptorId()),
                confirmFriendshipCommand.messageId());
    }

    /**
     * 删除一个朋友关系
     *
     * @param deleteFriendshipCommand command
     */
    public void deleteFriendship(DeleteFriendshipCommand deleteFriendshipCommand) {
        friendshipService().deleteFriendshipBetween(
                new MemberId(deleteFriendshipCommand.requesterId()),
                new MemberId(deleteFriendshipCommand.acceptorId()));
    }

    private FriendshipService friendshipService() {
        return friendshipService;
    }

    private MemberRepository memberRepository() {
        return memberRepository;
    }

}
